package com.midea.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Mr.Wang
 * @Description:
 * @Date: Create in 17:43 2021/10/31
 */
public class LeetCode17 {
    public static void main(String[] args) {
        String digits = "23";
        letterCombinations(digits);

    }

    public static List<String> letterCombinations(String digits) {
        List<String> res = new ArrayList<>();
        dfs(digits,0,new StringBuffer(),res);
        return res;
    }

    static void dfs(String str, int index, StringBuffer sb, List<String> res){
        char[][] m = new char[][]{
                {}, {}, {'a', 'b', 'c'}, {'d', 'e', 'f'},
                {'g', 'h', 'i'}, {'j', 'k', 'l'}, {'m', 'n', 'o'},
                {'p', 'q', 'r', 's'}, {'t', 'u', 'v'}, {'w', 'x', 'y', 'z'}
        };
        if (str.length() == 0){
            return;
        }
        if (index == str.length()){
            res.add(sb.toString());
            return;
        }
        //charAt返回的是char类型，转换成ASCII码后可能导致数组越界
        for (char c : m[str.charAt(index) - '0']) {
            sb.append(c);
            dfs(str,index + 1,sb,res);
            sb.deleteCharAt(sb.length()-1);
        }
    }

}
